package com.itheima.stock.remotedata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.stock.common.constance.JsonConstance;
import com.itheima.stock.job.service.StockTimerService;
import com.itheima.stock.pojo.StockMarketIndexInfo;
import com.itheima.stock.pojo.StockMarketLogPrice;
import com.itheima.stock.utils.IdWorker;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@SpringBootTest
public class PullRemoteData {
    @Autowired
    private IdWorker idWorker;

    @Autowired
    private StockTimerService stockTimerService;
    @Test
    public void test002(){
        try {
            stockTimerService.insertRTBatch();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void tset02() throws ParseException {
      stockTimerService.insertBatch();
    }

    @Test
    public void tset01() throws ParseException {
        Map<String, List> resultMap = new HashMap();
        String dapanjson= JsonConstance.dapanJson;
        Map map = JSON.parseObject(dapanjson, Map.class);
        //System.out.println(map);
        JSONArray resultJsonArray = (JSONArray) map.get("result");
        // 定义集合用于收集大盘分钟趋势数据
        List<StockMarketIndexInfo> stockIndexInfoList = new ArrayList<>();
        // 定义集合用于收集大盘基础数据 前收盘价 今开盘价
        List<StockMarketLogPrice> stockLogList = new ArrayList<>();
        // 遍历大盘数据数组
        for (Object resultJson : resultJsonArray) {
            // 将获取的元素转成Json对象
            JSONObject jsonObj =  (JSONObject)resultJson;
            // 大盘名称
            String name = jsonObj.get("name").toString();
            System.out.println("--------------"+name+"-------------");
            System.out.println("大盘名称 "+name);
            // 大盘代码
            String code = jsonObj.get("code").toString();
            System.out.println("大盘代码 "+code);
            // 处理大盘代码
            if (name.equals("上证指数")){
                code = "s_sh"+code;
            }else if(name.equals("深证成指")){
                code = "s_sz"+code;
            }else {
                code = "s_qt"+code;
            }
            BigDecimal price = new BigDecimal(jsonObj.get("price").toString());
            System.out.println("大盘当前价格 "+price);
            // 大盘开盘价格
            BigDecimal openningprice = new BigDecimal(jsonObj.get("openningprice").toString());
            System.out.println("大盘开盘 "+openningprice);
            // 收盘价格
            BigDecimal lastclosingprice = new BigDecimal(jsonObj.get("lastclosingprice").toString());
            System.out.println("大盘前收盘价格 "+lastclosingprice);
            // 返回的趋势数据条数
            String trendnum = jsonObj.get("trendnum").toString();
            System.out.println("大盘趋势数据数量 "+trendnum);
            // 当前日期
            Date updatetime = new DateTime(Long.parseLong(jsonObj.get("updatetime") + "000")).toDate();
            System.out.println("当前时间 "+updatetime);
            JSONArray trendArray = (JSONArray) jsonObj.get("trend");
            System.out.println("大盘趋势数据 "+trendArray);
            for (Object o : trendArray) {
                StockMarketIndexInfo stockMarketIndexInfo = new StockMarketIndexInfo();
                // 将每一分钟生成的数据转成字符串
                String trend = (String)o;
                // 处理
                String[] trendArr = trend.split(",");
                String curTime = trendArr[0];
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date date = dateFormat.parse(curTime);
                BigDecimal curPrice = new BigDecimal(trendArr[1]);
                String tradeAccount = trendArr[2];
                String tradeVolume = trendArr[3];
                stockMarketIndexInfo.setId(idWorker.nextId()+"");
                stockMarketIndexInfo.setMarkId(code);
                stockMarketIndexInfo.setCurTime(date);
                stockMarketIndexInfo.setMarkName(name);
                stockMarketIndexInfo.setCurPoint(curPrice);
                // 当前价格 - 前一日收盘价格
                stockMarketIndexInfo.setCurrentPrice(curPrice.subtract(lastclosingprice)); // 涨跌额
                // (当前价格 - 前一日收盘价格)/前一日收盘价格
                stockMarketIndexInfo.setUpdownRate((curPrice.subtract(lastclosingprice)).divide(lastclosingprice,4,BigDecimal.ROUND_HALF_UP)); // 涨跌幅
                stockMarketIndexInfo.setTradeAccount(tradeAccount);
                stockMarketIndexInfo.setTradeVolume(tradeVolume);
                stockIndexInfoList.add(stockMarketIndexInfo);
            }
            StockMarketLogPrice stockMarketLogPrice = new StockMarketLogPrice();
            stockMarketLogPrice.setId(idWorker.nextId()+"");
            stockMarketLogPrice.setMarketCode(code);
            stockMarketLogPrice.setCurDate(updatetime);
            stockMarketLogPrice.setPreClosePrice(lastclosingprice);
            stockMarketLogPrice.setOpenPrice(openningprice);
            stockLogList.add(stockMarketLogPrice);
        }
        System.out.println(stockIndexInfoList);
        System.out.println("插入成功...");

    }
}
